class Dom {
    constructor(domStr) {
        this.domList = Array.from(document.querySelectorAll(domStr));
    }

    removeClass(className) {
        this.domList.forEach(dom => {
            dom.classList.remove(className);
        })
        return this;
    }

    addClass(className) {
        this.domList.forEach(dom => {
            dom.classList.add(className);
        })
        return this;
    }


    on(evtName, evtFunc, isCatch = true) {

        document.addEventListener(evtName, (e) => {
            this.domList.forEach(dom => {
                if (e.target == dom) {
                    evtFunc.call(e.target);
                }
            })
        }, isCatch);
    }

    toggleClass(className, isAddClass = false) {
        if (isAddClass) {
            this.domList.forEach(dom => {
                dom.classList.add(className);
            })
        } else {
            this.domList.forEach(dom => {
                let hasClassName = Array.from(dom.classList).some(function (curCls) {
                    return curCls === className;
                });
                if (hasClassName) {
                    dom.classList.remove(className);
                } else {
                    dom.classList.add(className);
                }
            });
        }
    }


    attr(...args) {
        if (args.length > 1) {
            this.domList.forEach(dom => {
                dom.setAttribute(args[0], args[1]);
            })
        } else {
            this.domList.forEach(dom => {
                dom.getAttribute(args[0]);
            })
        }
    }

}


function $(domStr) {
    return new Dom(domStr);
}

export default $;